package com.qcxy.gzy.task;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.widget.Toast;

import com.qcxy.gzy.activity.FirstPageActivity;
import com.qcxy.gzy.activity.R;
import com.qcxy.gzy.activity.ScanActivity;
import com.qcxy.gzy.app.ComApplication;
import com.qcxy.gzy.app.MNDFitness;
import com.qxcy.gzy.util.SPUtils;

/**
 * 获取二维码图片Task
 *
 * @author gzy
 */
public class GetUserNameTask extends AsyncTask<Void, Void, JSONObject> {
    private ScanActivity mActivity = null;

    public GetUserNameTask(ScanActivity activity) {
        mActivity = activity;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    // 新开启的线程要执行的业务逻辑执行的方法
    @Override
    protected JSONObject doInBackground(Void... params) {
        String mac = ComApplication.getLocalMacAddressFromWifiInfo();
        JSONObject jObject = MNDFitness.scanLogin(mac);
        return jObject;
    }

    // 要更新的UI写在此处
    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        if (result != null) {
            int resultCode = result.optInt("success");
            if (resultCode == MNDFitness.RESULT_CODE) {// 返回正常
                // 更新UI解析数据
                JSONObject jsonObject;
                try {
                    jsonObject = result.getJSONObject("data");
                    if (jsonObject == null) {
                        return;
                    }
                    SPUtils.setValueSharedFile(mActivity, "userName", jsonObject.optString("userName"));
                    SPUtils.setValueSharedFile(mActivity, "OpenId", jsonObject.optString("openid"));
                    SPUtils.setValueSharedFile(mActivity, "URL", jsonObject.optString("headimgurl"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if ("".equals(SPUtils.getValueFromSharedFile(mActivity, "userName", ""))) {
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        public void run() {
                            new GetUserNameTask(mActivity).execute();
                        }
                    }, 2000);
                } else {
                    //v2
                    Intent intent = new Intent(mActivity, FirstPageActivity.class);
                    mActivity.startActivity(intent);
                    mActivity.finish();
                }
            } else if (resultCode == MNDFitness.RESULT_CODE_NOINFO) {
                Toast.makeText(mActivity, R.string.net_exception, Toast.LENGTH_SHORT).show();
            } else {// 返回失败
                Toast.makeText(mActivity, R.string.server_exception, Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(mActivity, R.string.server_fail,
                    Toast.LENGTH_SHORT).show();
        }
    }
}
